package com.briup.web;

import com.briup.pojo.Book;
import com.briup.pojo.Category;
import com.briup.service.IBookService;
import com.briup.service.ICategotyService;
import com.briup.serviceImp.BookServiceImp;
import com.briup.serviceImp.CategoryServiceImp;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import java.util.List;


public class ApplicationListener implements ServletContextListener
        {

    // Public constructor is required by servlet spec
    public ApplicationListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        ServletContext context = sce.getServletContext();
        //查询出来所有的一级分类和二级分类
        ICategotyService service = new CategoryServiceImp();
        List<Category> list = service.findAllCategories();
        //查询出来所有的书籍信息
        IBookService bookservice = new BookServiceImp();
        List<Book> books = bookservice.findAllBooks();
        //向整个应用中添加分类信息list和书籍信息books
        context.setAttribute("list", list);
        context.setAttribute("books", books);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attribute
         is replaced in a session.
      */
    }
}
